import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const initialState = {
  count: 0, 
  price: 10.5,
  sum: 0,
  channelList: []  
};


const counterSilce = createSlice({
  name: 'counter', 
  initialState, 
  reducers: { 
    increment(state, { payload }) {
      state.count += payload
      state.sum = state.count * state.price
    },
    decrement(state) {
      state.count -= 1
    },
    setChannerlList(state, { payload }) {
      state.channelList = payload
    }
  }
})

const fetchChannels = () => {
  return async (dispatch, getState) => {
    console.log(getState().counter, '1111')
    let res = await axios.get('http://blogapi.shbwyz.com/article/list')
    console.log(res.data.rows)
    dispatch(setChannerlList(res.data.rows))
  }
}
export { fetchChannels }
export const { increment, decrement, setChannerlList } = counterSilce.actions
export default counterSilce.reducer